package com.emm.yixun.mobile.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.adapter.NoticeAdapter;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.GetRemindList;
import com.emm.yixun.mobile.model.GetRemindList.RemindList;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import xf.tools.Loading;

/**
 * 
 * <p>
 * <b>Title：</b>提醒
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午4:06:47
 */
public class RemindActivity extends SwipeBackActivity {

	private static final String TAG = "RemindActivity";

	/** 提醒列表 */
	ListView notice_listview;

	/** 适配器 */
	NoticeAdapter adapter;

	/** 提醒列表数据 */
	ArrayList<RemindList> list;

	/** 标题 */
	TextView title_main;

	/** 返回按钮 */
	ImageView back_btn;

	/** 侧滑关闭框架 */
	private SwipeBackLayout mSwipeBackLayout;

	@Override
	protected void onResume() {
		BaseActivity.activity = this; // TODO Auto-generated method stub
		getRemindList();
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notices_activity);

		SetSwipeBackLayout(mSwipeBackLayout);

		BaseActivity.activity = this;

		initView();

		SetRelativiLayoutPadding(R.id.main_head_top_rela);
		
		initBtn();
	}

	/**
	 * <p>
	 * <b>Title：</b> 初始化view
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月21日 下午4:18:30
	 */
	private void initView() {
		notice_listview = (ListView) findViewById(R.id.notice_listview);
		title_main = (TextView) findViewById(R.id.title_main);
		title_main.setText(getString(R.string.notice_title_fd));
		back_btn = (ImageView) findViewById(R.id.back_btn);
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化监听事件
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:30:09
	 */
	private void initBtn() {
		back_btn.setVisibility(View.VISIBLE);
		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		notice_listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent();
				intent.putExtra("TITLE", getremindlis.getRemindList().get(position).getRemindCategory());
				intent.putExtra("remindCategoryId", getremindlis.getRemindList().get(position).getRemindCategoryId());
				intent.setClass(RemindActivity.this, RemindContentActivity.class);
				startActivity(intent);
			}
		});
	}

	GetRemindList getremindlis;

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 提醒列表查询接口
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午4:30:24
	 */
	private void getRemindList() {
		EmmApplication.updateUrl("getRemindList");
		Map<String, String> map = new HashMap<String, String>();
		map.put("version", EmmApplication.getPackageManager(5));
		map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
		map.put("token", EmmApplication.getToken());
		map.put("userId", EmmApplication.getUserId());
		map.put("terminalCode", EmmApplication.getPackageManager(2));
		map.put("projectCode", EmmApplication.getProjectCode());
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("params", map);
		JSONObject json = new JSONObject(paramsMap);
		String jsonstr = json.toString();
		RequestParams params = new RequestParams();
		params.put("body", jsonstr);
		Log.v("getRemindList-->post", EmmApplication.urlStr + "?body=" + jsonstr);
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束error:" + error + "==" + content);
			}

			@Override
			public void onFinish() {
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束");
				super.onFinish();
			}

			@Override
			public void onStart() {
				super.onStart();
				Loading.hideDialogForLoading();
				Loading.showDialogForLoading(RemindActivity.this, "正在加载...", false, true, 60000);
				Log.v(TAG, "请求开始");
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				if (content == null) {
					return;
				}
				Log.v("content", "content=>" + content);
				JSONObject jsonObj = JSONObject.parseObject(content);
				JSONObject resData = (JSONObject) jsonObj.get("resData");
				if (resData == null) {
					Log.v(TAG, "信息返回值为空");
					return;
				}

				getremindlis = JSONObject.parseObject(resData.toString(), GetRemindList.class);

				if ("1".equals(getremindlis.getResult())) {
					Log.v("获取成功", "");
					if (null != getremindlis && null != getremindlis.getRemindList()) {
						if (null != adapter) {
							adapter.GetDate(getremindlis.getRemindList());
						} else {
							adapter = new NoticeAdapter(RemindActivity.this, getremindlis.getRemindList());
							notice_listview.setAdapter(adapter);
						}
					} else {
						list = new ArrayList<GetRemindList.RemindList>();
						if (null != adapter) {
							adapter.GetDate(list);
						} else {
							adapter = new NoticeAdapter(RemindActivity.this, list);
							notice_listview.setAdapter(adapter);
						}
					}
				} else {
					EmmApplication.T(getremindlis.getErrorMsg().toString());
					Log.v("获取失败", "errorCode:" + getremindlis.getErrorCode().toString() + "errorMsg:"
							+ getremindlis.getErrorMsg().toString());
				}
			}
		});
	}

}
